<?php 
header("Content-type: text/html; charset=utf-8"); 
putenv('TZ=Asia/Shanghai');
require_once 'class/Cache.php';
include_once('config.php');
include_once('class/DB_Mysql.class.php');
$cache=new Cache('./cache');
$set=$_POST['set'];
$catid=$_POST['catid'];
$action=$_POST['action'];


//$categoryName=array('2E03'=>'港股资讯','2491'=>'市场评测','24B3'=>'期货资迅');

$mysql=new DB_Mysql();
$mysql->connid=mysql_connect('localhost:3308','root','123456');
$mysql->select_db('phpcms');
$mysql->query('set names gbk');
if($action=='update') //更新设置
{
	$mysql->query('set names utf8');
	$sql='SELECT uset FROM php_member_set WHERE uid='.gCookie('userid');
	$rs=$mysql->select($sql);
	$set=str_replace('\"','"',trim($set));
	if(isset($rs[0]['uset']))
	{
		$sql='UPDATE php_member_set SET mtime='.time().',uset=\''.$set.'\' WHERE uid='.gCookie('userid');
	}
	else
	{
		$sql='INSERT INTO php_member_set(uid,mtime,uset) VALUES('.gCookie('userid').','.time().',\''.$set.'\')';
	}

	$mysql->query($sql) or die('error');exit('1');
}


$catids=str_replace('\"','"',urldecode($catid));
$catids=my_json_decode($catids);

include_once 'class/DB_Mssql.class.php';
$mssql=new DB_mssql();
$mssql->connect('222.73.91.35','sa','tsts168','VsatData');

$json=array();
foreach($catids as $value)
{
	$key=$value['key'];
	$rs='';
	if($value['show']<0)
	{
		continue;
	}
	if($key=='sjzl')//数据专栏
	{
		$rs=getCjsj($value['name']);
	}
	else if($key=='fav')
	{
		$sql='select \''.$value['name'].'\' as catname,concat("/collect.php?userid=",b.userid) as curl,a.title,b.addtime as updatetime,"fav" as catid,b.contentid as id,a.url from phpcms_content a,phpcms_collect b where a.contentid=b.contentid and b.userid='.$value['userid'].' order by addtime limit '.$value['num'];
		$rs=cacheSql($sql,$mysql,1);
		$rs=setFormat($rs);
	}
	else if($value['database']==='0')
	{
		$sql='select \''.$value['name'].'\' as catname,curl,title,updatetime as mtime,catid,contentid as id,url from articles where catid='.$key.' and status=99 order by listorder desc,updatetime desc limit '.$value['num'];
		$rs=cacheSql($sql,$mysql);
		$rs=setFormat($rs);
	}
	else if($value['database']==='1')
	{
		$sql='SELECT TOP '.$value['num'].' \''.$value['name'].'\' as catname, wj as id,convert(varchar(100),gxrq,120) as mtime,lb as catid,\'category.php?id=\'+lb as curl, \'article.php?id=\'+wj as url,bt as title FROM zx_articles WHERE lb = \''.$key.'\' order by gxrq desc';//echo $sql;//catname,curl,title,mtime,catid,id,url
		$rs=cacheSql($sql,$mssql);//$mssql->select($sql);
		$rs=setFormat($rs);
	}
	//echo $sql.'<br>';
	$json[]=$rs;
}
function setFormat($rs)
{
	if(!is_array($rs)){return array();}
	$json=array();
	foreach($rs as $value)
	{
		$id=$value['id'];
		$json['name']=iconv('GBK','UTF-8',$value['catname']);
		$json['curl']=((strpos($value['curl'],'collect')!==false)?'cms/':'').$value['curl'];
		$json['articles'][$id]['title']=iconv('GBK','UTF-8',$value['title']);
		$json['articles'][$id]['url']='cms/'.$value['url'];
		$json['key']=$value['catid'];
		if(strlen($value['mtime'])==10)
		{
			$json['articles'][$id]['time']=date('Y-m-d H:i:s',$value['mtime']);
		}
		else
		{
			$json['articles'][$id]['time']=$value['mtime'];
		}
	}
	return $json;
}

function getCjsj($catename)//更新数据资讯
{
	$cacheName='sjzl';
	$cache=$GLOBALS['cache'];
	if(time()/86400-$cache->cache_time($cacheName)/86400>1)
	{
	$json['key']='sjzl';
	$json['name']=$catename;
	$json['curl']='';
	$titles=array('特别提示'=>array('%日特别提示','21F0'),
					'新股发行上市'=>array('新股发行上市'),
					'沪市市盈率排行榜'=>array('沪市%市盈率排行榜'),
					'深市市盈率排行榜'=>array('深市%市盈率排行榜'),
					'沪市大宗交易报告'=>array('%沪市大宗交易信息','21F0'),
					'深市大宗交易报告'=>array('%深市大宗交易信息','21F0'),
					'沪市成交回报'=>array('沪市涨跌幅超过7%信息'),
					'深市成交回报'=>array('深市涨跌幅超过7%信息'),
					'中小企业版成交回报'=>array('中小企业版7%信息'),
					'增发配股再融资股'=>array('%最新发行、上市、增发、配股'),
					'限售解禁统计'=>array('%'.date('Y').'年'.date('m').'月份股改限售股份%'),
					'AB股价对照表'=>array('%AB股价对比表%'),
					'A+H股价对照表'=>array('%A+H股价对比表%'),
					'沪市涨幅排行榜'=>array('沪市%涨幅排行榜'),
					'深市涨幅排行榜'=>array('深市%涨幅排行榜'),
					'沪市跌幅排行榜'=>array('沪市%跌幅排行榜'),
					'深市跌幅排行榜'=>array('深市%跌幅排行榜'),
					'沪市振幅排行榜'=>array('沪市%振幅排行榜'),
					'深市振幅排行榜'=>array('深市%振幅排行榜'),
					'沪市换手率排行榜'=>array('沪市%换手率排行榜'),
					'深市换手率排行榜'=>array('深市%换手率排行榜'),
					'沪市成交量排行榜'=>array('沪市%成交量排行榜'),
					'深市成交量排行榜'=>array('深市%成交量排行榜')
					);
		foreach($titles as $title=>$keys)
		{
			$id=isset($keys[1])?articles($keys[0],$keys[1]):articles($keys[0]);
			$json['articles'][$id]['title']=$title;
			$json['articles'][$id]['url']='cms/article.php?id='.$id.'&mssql=2';
		}
		$json['articles'][$id]['time']='';
		$cache->store($cacheName,$json);
	}
	else
	{
		$json=$cache->fetch($cacheName);
	}
	return $json;
}


function articles($text,$key=null)
{
	$text=iconv('UTF-8','GBK',$text);
	if($text!=null)$text=(strpos($text,'%')!==false)?('and bt like \''.$text.'\''):(' and bt=\''.$text.'\'');
	$sql='select top 1 wj from zx_articles where gxrq>\'2009-09-01\' '.$text;
	if($key!==null){$sql.=' and lb=\''.$key.'\'';}
	$rs=$GLOBALS['mssql']->select($sql);
	return trim($rs[0]['wj']);
}

function cacheSql($sql,$conn,$time=100,$type='select')
{
	$cache_name=$sql;
	$cache=$GLOBALS['cache'];
	if(time()-$cache->cache_time($cache_name)<$time)
	{
		return $cache->fetch($cache_name);
	}
	else
	{
		$content=$conn->$type($sql);
		$cache->store($cache_name,$content);
		return $content;
	}
}


$json=my_json_encode($json);
$cache->store($cache_name,$json);
echo $json;
?>